/**
 * Application Application.java
 */

package CCT220_Aula20_Exercicio02_13106;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


/**
 * <div style="border-style:double;">
 * Project: CCT220
 * </br>Package: CCT220_Aula20_Exercicio02_13106
 * </br>Class: Application
 * </br>Created by marcel
 * </br>Description:Browser
 * </div>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class Application {

	/**
	 * JEditorPane jepBrowser
	 */
	private JEditorPane jepBrowser;

	/**
	 * JTextField jtfLink
	 */
	private JTextField  jtfLink;

	/**
	 * JButton jbGo
	 */
	private JButton     jbGo;

	/**
	 * JLabel jlLog
	 */
	private JLabel      jlLog;


	/**
	 * <div style="border-style:double;">
	 * Constructor for
	 * </br>CCT220_Aula20_Exercicio02_13106::Application
	 * </br>LastChange: 05/11/2011 10:25:16 by marcel
	 * </div>
	 * 
	 */
	public Application() {

		JFrame jf = new JFrame("Marcel M. Grilo - 13106");
		jf.setLayout(new BorderLayout(3, 3));
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setSize(800, 600);

		this.startComponents();
		this.startListeners();

		jf.add(this.montaTopo(), BorderLayout.PAGE_START);
		jf.add(new JScrollPane(this.jepBrowser), BorderLayout.CENTER);
		jf.add(this.jlLog, BorderLayout.PAGE_END);

		jf.setVisible(true);
	}


	/**
	 * <div style="border-style:double;">
	 * Metho:CCT220_Aula20_Exercicio02_13106::Application::startComponents
	 * </br>Last Change: 05/11/2011 10:32:26 by marcel
	 * </br>Description: Inicializa os componentes
	 * </div>
	 * 
	 */
	private void startComponents() {

		jtfLink = new JTextField();
		jbGo = new JButton("Go!");
		jepBrowser = new JEditorPane();
		jlLog = new JLabel("Log: ");

	}


	/**
	 * <div style="border-style:double;">
	 * Metho:CCT220_Aula20_Exercicio02_13106::Application::startListeners
	 * </br>Last Change: 05/11/2011 10:33:53 by marcel
	 * </br>Description: Inicializa os listeners dos componentes
	 * </div>
	 * 
	 */
	private void startListeners() {

		jbGo.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				jlLog.setText("Log: " + goToPage(jtfLink.getText()));
			}
		});
		jtfLink.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {

			}


			@Override
			public void keyReleased(KeyEvent e) {

				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					jbGo.doClick();
				}
			}


			@Override
			public void keyPressed(KeyEvent e) {

			}
		});

	}


	/**
	 * <div style="border-style:double;">
	 * Metho:CCT220_Aula20_Exercicio02_13106::Application::goToPage
	 * </br>Last Change: 05/11/2011 10:40:00 by marcel
	 * </br>Description: Metodo que carrega uma pagina web no JEditorPane
	 * </div>
	 * 
	 * @param text
	 * @return aString
	 */
	protected String goToPage(String text) {

		if (!text.contains("http://")) {
			text = "http://" + text;
			jtfLink.setText(text);
		}
		try {
			jepBrowser.setPage(text);
			return "Pagina carregada!!!";
		} catch (Exception e) {
			return e.getMessage();
		}

	}


	/**
	 * <div style="border-style:double;">
	 * Metho:CCT220_Aula20_Exercicio02_13106::Application::montaTopo
	 * </br>Last Change: 05/11/2011 10:30:26 by marcel
	 * </br>Description: Metodo que monta a parte superior da janela
	 * </div>
	 * 
	 * @return
	 */
	private Component montaTopo() {

		JPanel jp = new JPanel(new BorderLayout(3, 3));
		jp.add(jtfLink, BorderLayout.CENTER);
		jp.add(jbGo, BorderLayout.LINE_END);

		return jp;
	}


	/**
	 * <div style="border-style:double;">
	 * Metho:CCT220_Aula20_Exercicio02_13106::Application::main
	 * </br>Last Change: 05/11/2011 10:24:32 by marcel
	 * </br>Description: Metodo estatico que inicializa a classe
	 * </div>
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		new Application();

	}
}
